function str2hex(str){
	var ret = "";
	for(var i = 0; i < str.length; i ++){
		var c = str.charCodeAt(i).toString(16);
		if(c.length < 2)
			c = "0" + c;
		ret += c;
	}
	
	return ret;
}

function ary2Str(ary){
	return String.fromCharCode.apply(null,ary);
}

function hex2Str(strInHex){
	if(strInHex.length % 2 != 0)
		return null;

	var loop = strInHex.length / 2;

	var ary = new Array();

	for(var i = 0; i < loop; i ++){
		var hex = strInHex.substr(i * 2, 2);
		ary.push(parseInt(hex, 16));
	}

	return ary2Str(ary);
}

function ary2Hex(ary){
	var ret = "";
	
	for(var i = 0; i < ary.length; i ++){
		var c = ary[i].toString(16);
		if(c.length < 2)
			c = "0" + c;
		ret += c;
	}
	
	return ret;
}

function extractModulus(hexPubKey){
	if(hexPubKey.length != 280)
		return null;
	
	var modulus = hexPubKey.substr(14, 256);
	return modulus;
}

function extractExp(hexPublicKey){
	if(hexPublicKey.length != 280)
		return null;

	return hexPublicKey.substr(-6);
}

function initKey(aKey) {
		var state = new Array(); 

		for (var i = 0; i < 256; i++) {
			state[i] = i & 0xff;
		}
		var index1 = 0;
		var index2 = 0;
		if (!aKey) {
			return null;
		}
		for (var i = 0; i < 256; i++) {
			index2 = ((aKey.charCodeAt(index1) & 0xff) + (state[i] & 0xff) + index2) & 0xff;
			var tmp = state[i];
			state[i] = state[index2];
			state[index2] = tmp;
			index1 = (index1 + 1) % aKey.length;
		}

		return state;
}

function RC4Base(input, mkey) {
		var x = 0;
		var y = 0;
		var skey = initKey(mkey);
		var xorIndex;
	  var result = new Array(input.length);

		for (var i = 0; i < input.length; i++) {
			x = (x + 1) & 0xff;
			y = ((skey[x] & 0xff) + y) & 0xff;
			var tmp = skey[x];
			skey[x] = skey[y];
			skey[y] = tmp;
			xorIndex = ((skey[x] & 0xff) + (skey[y] & 0xff)) & 0xff;
			result[i] = (input.charCodeAt(i) ^ skey[xorIndex]) & 0xff;
		}

		return result;
}

function rc4ResultToB64(result){
	var binRet = ary2Str(result);
	return Base64.encode(binRet);
}

function strToCharCode(str){
	var ary = new Array();
	for(var i = 0; i < str.length; i ++){
		var code = str.charCodeAt(i);
		ary.push(code);
	}
	
	return ary;
}

function randomByte() {
	var b = 0;
	for(var i = 0; i < 8; i ++){
		var selector = Math.random() > 0.5 ? 1 : 0;

		b += selector * Math.pow(2, i);
	}

	return b;
}

